package com.qf.huigou.service.impl;

import com.qf.huigou.mapper.GoodMapper;
import com.qf.huigou.mapper.SubPropertyMapper;
import com.qf.huigou.pojo.Good;
import com.qf.huigou.pojo.ParProperty;
import com.qf.huigou.pojo.SubProperty;
import com.qf.huigou.service.PropertyService;
import com.qf.huigou.utils.ParseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.8.0_131
 * @Author WangYiZhu
 * @Email 2641956765@qq.com
 * @Time 2022/12/08 19:16
 */
@Service
public class PropertyServiceImpl implements PropertyService {

    @Autowired
    private SubPropertyMapper subPropertyMapper;
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public int getPriceToGood(int goodId, int[] subTypeIds) {
        List<Integer> list=new ArrayList<>();
        //获取到当前的商品
        Good good=goodMapper.selectByPrimaryKey(Long.valueOf(goodId));
        int price=subPropertyMapper.selectGoodZcPrice(subTypeIds);
        int basicPrice=good.getGoodCost()-price;
        if (basicPrice<good.getGoodMinPrice()){
            basicPrice=good.getGoodMinPrice();
        }

        return basicPrice;
    }

    @Override
    public List<Map<ParProperty, List<SubProperty>>> getMapListToGoodId(int goodId) {

        List<Map<ParProperty, List<SubProperty>>> mapList = new ArrayList<Map<ParProperty, List<SubProperty>>>();
        List<SubProperty> subPropertyList = subPropertyMapper.selectSubPropertyToGoodId(goodId);
        /***************************这一部分等价于查询数据库中的表basic_info表***************************************/
        List<ParProperty> propertyList = new ArrayList<ParProperty>();
        for (SubProperty subProperty : subPropertyList) {
            propertyList.add(subProperty.getParProperty());
        }

        //去重，重复是通过对象比较去重，这里不对
        //需要重写equles方法和hashcode方法
        propertyList = propertyList.stream().distinct().collect(Collectors.toList());
       /**********************************************************/
        Map<ParProperty, List<SubProperty>> basicMap = new HashMap<ParProperty, List<SubProperty>>();
        for (ParProperty parProperty : propertyList) {
            //每一个parProperty都对应的是Map中的一个key ，这个key都对应一个SubProperty的集合
            //每循环一次就得到一个key，意味着有一个先集合
            List<SubProperty> valueList = new ArrayList<SubProperty>();
            for (SubProperty subProperty : subPropertyList) {
                if (parProperty.getParPropertyId() == subProperty.getParProperty().getParPropertyId()) {
                    valueList.add(subProperty);
                }
            }
            //到这里说明当前parProperty中的值找完了
            basicMap.put(parProperty, valueList);

        }
        Map<ParProperty, List<SubProperty>> map1 = new HashMap<ParProperty, List<SubProperty>>();
        Map<ParProperty, List<SubProperty>> map2 = new HashMap<ParProperty, List<SubProperty>>();
        Map<ParProperty, List<SubProperty>> map3 = new HashMap<ParProperty, List<SubProperty>>();

        for (ParProperty parProperty : basicMap.keySet()) {
            if (parProperty.getParPropertyId() < 7|| parProperty.getParPropertyId()==13) {
                map1.put(parProperty, basicMap.get(parProperty));
            } else if (parProperty.getParPropertyId() == 7) {
                map2.put(parProperty, basicMap.get(parProperty));
            } else if (parProperty.getParPropertyId()>7&&parProperty.getParPropertyId()!=13){
                map3.put(parProperty, basicMap.get(parProperty));
            }
        }
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        return mapList;
    }
}
